package utils

import (
	"fmt"
	"github.com/apache/pulsar-client-go/pulsar"
	"pulsar-go/global"
)

var config = global.Application.Pulsar

func BuildClint() (pulsar.Client, error) {
	config := global.Application.Pulsar
	return pulsar.NewClient(pulsar.ClientOptions{
		URL:            fmt.Sprintf("pulsar://%s:%d", config.Host, config.Port),
		Authentication: pulsar.NewAuthenticationToken(config.Token),
	})
}

func BuildConsumer(client pulsar.Client) (pulsar.Consumer, error) {
	config := global.Application.Pulsar
	return client.Subscribe(pulsar.ConsumerOptions{
		Topic:            config.Topic,        // 替换为您的主题名称
		SubscriptionName: config.Subscription, // 替换为您的订阅名称
		Type:             pulsar.Shared,
	})
}

func BuildProducer(client pulsar.Client) (pulsar.Producer, error) {
	return client.CreateProducer(pulsar.ProducerOptions{
		Topic: config.Topic,
	})
}
